/**
 * 
 */
package com.arx.spell;

import com.arx.common.GlobalConsts;
import com.arx.interactive.InteractiveObject;

/**
 * @author Donald
 */
public final class Spell {
	/** the singleton instance. */
	private static Spell	instance;
	/**
	 * Gets the one and only instance of {@link Spell}.
	 * @return {@link Spell}
	 */
	public static Spell getInstance() {
		if (Spell.instance == null) {
			Spell.instance = new Spell();
		}
		return Spell.instance;
	}
	private SpellObject[]	spells	= new SpellObject[GlobalConsts.MAX_SPELLS];
	/** Hidden constructor. */
	private Spell() {}
	public int ARX_SPELLS_GetSpellOn(final InteractiveObject io,
			final int spellid) {
		int val = -1;
		if (io != null) {
			for (int i = 0; i < io.getNb_spells_on(); i++) {
				SpellObject spell = spells[io.getSpells_onItem(i)];
				if (spell != null
						&& spell.getType() == spellid
						&& spell.exists()) {
					val = io.getSpells_onItem(i);
					break;
				}
			}
		}
		return val;
	}
	public void ARX_SPELLS_RemoveAllSpellsOn(final InteractiveObject io) {
		io.freeSpellsOnMemory();
	}
	public boolean exists(final int index) {
		boolean exists = false;
		if (spells != null
				&& index >= 0
				&& index < spells.length) {
			exists = spells[index].exists();
		}
		return exists;
	}
	public int getCaster(final int index) {
		int caster = -1;
		if (spells != null
				&& index >= 0
				&& index < spells.length) {
			caster = spells[index].getCaster();
		}
		return caster;
	}
	public SpellObject getSpell(final int index) {
		SpellObject spell = null;
		if (spells != null
				&& index >= 0
				&& index < spells.length) {
			spell = spells[index];
		}
		return spell;
	}
	public short GetSpellId(final String spell) {
		short val = -1;
		if (spell.equalsIgnoreCase("ACTIVATE_PORTAL")) {
			val = GlobalConsts.SPELL_ACTIVATE_PORTAL;
		}
		if (spell.equalsIgnoreCase("DOUSE")) {
			val = GlobalConsts.SPELL_DOUSE;
		}
		if (spell.equalsIgnoreCase("IGNIT")) {
			val = GlobalConsts.SPELL_IGNIT;
		}
		if (spell.equalsIgnoreCase("MAGIC_SIGHT")) {
			val = GlobalConsts.SPELL_MAGIC_SIGHT;
		}
		if (spell.equalsIgnoreCase("MAGIC_MISSILE")) {
			val = GlobalConsts.SPELL_MAGIC_MISSILE;
		}
		// Level 2
		if (spell.equalsIgnoreCase("ARMOR")) {
			val = GlobalConsts.SPELL_ARMOR;
		}
		if (spell.equalsIgnoreCase("DETECT_TRAP")) {
			val = GlobalConsts.SPELL_DETECT_TRAP;
		}
		if (spell.equalsIgnoreCase("HARM")) {
			val = GlobalConsts.SPELL_HARM;
		}
		if (spell.equalsIgnoreCase("HEAL")) {
			val = GlobalConsts.SPELL_HEAL;
		}
		if (spell.equalsIgnoreCase("LOWER_ARMOR")) {
			val = GlobalConsts.SPELL_LOWER_ARMOR;
		}
		// Level 3
		if (spell.equalsIgnoreCase("CREATE_FOOD")) {
			val = GlobalConsts.SPELL_CREATE_FOOD;
		}
		if (spell.equalsIgnoreCase("DISPELL_ILLUSION")) {
			val = GlobalConsts.SPELL_DISPELL_ILLUSION;
		}
		if (spell.equalsIgnoreCase("FIREBALL")) {
			val = GlobalConsts.SPELL_FIREBALL;
		}
		if (spell.equalsIgnoreCase("ICE_PROJECTILE")) {
			val = GlobalConsts.SPELL_ICE_PROJECTILE;
		}
		if (spell.equalsIgnoreCase("SPEED")) {
			val = GlobalConsts.SPELL_SPEED;
		}
		// Level 4
		if (spell.equalsIgnoreCase("BLESS")) {
			val = GlobalConsts.SPELL_BLESS;
		}
		if (spell.equalsIgnoreCase("COLD_PROTECTION")) {
			val = GlobalConsts.SPELL_COLD_PROTECTION;
		}
		if (spell.equalsIgnoreCase("CURSE")) {
			val = GlobalConsts.SPELL_CURSE;
		}
		if (spell.equalsIgnoreCase("DISPELL_FIELD")) {
			val = GlobalConsts.SPELL_DISPELL_FIELD;
		}
		if (spell.equalsIgnoreCase("FIRE_PROTECTION")) {
			val = GlobalConsts.SPELL_FIRE_PROTECTION;
		}
		if (spell.equalsIgnoreCase("TELEKINESIS")) {
			val = GlobalConsts.SPELL_TELEKINESIS;
		}
		// Level 5
		if (spell.equalsIgnoreCase("CURE_POISON")) {
			val = GlobalConsts.SPELL_CURE_POISON;
		}
		if (spell.equalsIgnoreCase("LEVITATE")) {
			val = GlobalConsts.SPELL_LEVITATE;
		}
		if (spell.equalsIgnoreCase("POISON_PROJECTILE")) {
			val = GlobalConsts.SPELL_POISON_PROJECTILE;
		}
		if (spell.equalsIgnoreCase("REPEL_UNDEAD")) {
			val = GlobalConsts.SPELL_REPEL_UNDEAD;
		}
		if (spell.equalsIgnoreCase("RUNE_OF_GUARDING")) {
			val = GlobalConsts.SPELL_RUNE_OF_GUARDING;
		}
		// Level 6
		if (spell.equalsIgnoreCase("CREATE_FIELD")) {
			val = GlobalConsts.SPELL_CREATE_FIELD;
		}
		if (spell.equalsIgnoreCase("DISARM_TRAP")) {
			val = GlobalConsts.SPELL_DISARM_TRAP;
		}
		if (spell.equalsIgnoreCase("PARALYSE")) {
			val = GlobalConsts.SPELL_PARALYSE;
		}
		if (spell.equalsIgnoreCase("RAISE_DEAD")) {
			val = GlobalConsts.SPELL_RISE_DEAD;
		}
		if (spell.equalsIgnoreCase("SLOWDOWN")) {
			val = GlobalConsts.SPELL_SLOW_DOWN;
		}
		// Level 7
		if (spell.equalsIgnoreCase("CONFUSE")) {
			val = GlobalConsts.SPELL_CONFUSE;
		}
		if (spell.equalsIgnoreCase("FIRE_FIELD")) {
			val = GlobalConsts.SPELL_FIRE_FIELD;
		}
		if (spell.equalsIgnoreCase("FLYING_EYE")) {
			val = GlobalConsts.SPELL_FLYING_EYE;
		}
		if (spell.equalsIgnoreCase("ICE_FIELD")) {
			val = GlobalConsts.SPELL_ICE_FIELD;
		}
		if (spell.equalsIgnoreCase("LIGHTNING_STRIKE")) {
			val = GlobalConsts.SPELL_LIGHTNING_STRIKE;
		}
		// Level 8
		if (spell.equalsIgnoreCase("ENCHANT_WEAPON")) {
			val = GlobalConsts.SPELL_ENCHANT_WEAPON;
		}
		if (spell.equalsIgnoreCase("EXPLOSION")) {
			val = GlobalConsts.SPELL_EXPLOSION;
		}
		if (spell.equalsIgnoreCase("INVISIBILITY")) {
			val = GlobalConsts.SPELL_INVISIBILITY;
		}
		if (spell.equalsIgnoreCase("LIFE_DRAIN")) {
			val = GlobalConsts.SPELL_LIFE_DRAIN;
		}
		if (spell.equalsIgnoreCase("MANA_DRAIN")) {
			val = GlobalConsts.SPELL_MANA_DRAIN;
		}
		// Level 9
		if (spell.equalsIgnoreCase("INCINERATE")) {
			val = GlobalConsts.SPELL_INCINERATE;
		}
		if (spell.equalsIgnoreCase("MASS_PARALYSE")) {
			val = GlobalConsts.SPELL_MASS_PARALYSE;
		}
		if (spell.equalsIgnoreCase("NEGATE_MAGIC")) {
			val = GlobalConsts.SPELL_NEGATE_MAGIC;
		}
		if (spell.equalsIgnoreCase("SUMMON_CREATURE")) {
			val = GlobalConsts.SPELL_SUMMON_CREATURE;
		}
		if (spell.equalsIgnoreCase("FAKE_SUMMON")) {
			val = GlobalConsts.SPELL_FAKE_SUMMON;
		}
		// Level 10
		if (spell.equalsIgnoreCase("CONTROL")) {
			val = GlobalConsts.SPELL_CONTROL_TARGET;
		}
		if (spell.equalsIgnoreCase("FREEZE_TIME")) {
			val = GlobalConsts.SPELL_FREEZE_TIME;
		}
		if (spell.equalsIgnoreCase("MASS_INCINERATE")) {
			val = GlobalConsts.SPELL_MASS_INCINERATE;
		}
		if (spell.equalsIgnoreCase("MASS_LIGHTNING_STRIKE")) {
			val = GlobalConsts.SPELL_MASS_LIGHTNING_STRIKE;
		}
		return val;
	}
	public int getType(final int index) {
		int type = -1;
		if (spells != null
				&& index >= 0
				&& index < spells.length) {
			type = spells[index].getType();
		}
		return type;
	}
	public void setTolive(final int index, final long val) {
		if (spells != null
				&& index >= 0
				&& index < spells.length) {
			spells[index].setTolive(val);
		}
	}
}
